<?php


namespace app\pay\controller;


use think\Controller;
use think\Db;
use app\pay\service\Hqdf;

class Payhqdf extends Base
{
	public function pay($profit_id)
	{
		$profit = Db::name("UserProfit")->find($profit_id);
		if (empty($profit)) {
			return "订单不存在";
		}
		if($profit["profit_type"]>1){
			return "订单不存在";
		}
		$uid = $profit["profit_uid"];
		$card_id = $profit["profit_card_id"];
		$user_card = Db::name("user_card")->where(["card_id"=>$card_id])->find();
		$bank = Db::name("bank_list")->where(["list_id"=>$user_card["card_bank_id"]])->find();

		$data = [];
		$data["ordersn"]=get_order_sn("",$uid);//流水号
		$data["dsorderid"]=$profit["profit_form_no"];//商户订单号
		$data["username"] = $user_card["card_name"];//姓名
		$data["bankcard"] = $user_card["card_no"];//银行卡号/账号
		$data["amount"] = $profit["profit_true_money"];//金额
		$data["provinceCode"] = $this->getCityCodeByRegionOne($user_card["card_province"]);//开户行所在省份编码
		$data["cityCode"] = $this->getCityCodeByRegionOne($user_card["card_city"]);//开户行所在城市编码
		$data["bankname"] = $bank["list_name"];//开户行总行名称
		$data["bankid"] = $bank["list_code"];//开户行总行行号
		$data["bankcodename"] = $user_card["card_branch"];//开户行名称
		$data["bankcode"] = $bank["list_unionpay_no"];//开户行联行号
		$data["remark"] = "";//备注
		$data["notifyUrl"] = config("app_host")."/pay/Payhqdf/pay_notify";//异步通知地址
		$res = (new Hqdf())->pay($data);
		//halt($res);
		$update=[];
		$update["profit_paytime"]=time();
		$update["profit_back_msg"]=isset($res["errtext"]) ? $res["errtext"] : "成功";
		if($res["returncode"]=="0039"){//余额不足
			$update["profit_type"]=$res["returncode"];
		}elseif($res["returncode"] == "0003"){//交易处理中
			$update["profit_type"]=4;
		}elseif($res["returncode"] == "0000"){
			$update["profit_type"]=2;
		}elseif($res["returncode"] == "0001"){
			$update["profit_type"]=3;
		}

		if(isset($res["frozenamount"]) && $res["frozenamount"]>0){
			$update["frozenamount"] = $res["frozenamount"];
		}
		$upres = Db::name("user_profit")->where(["profit_id"=>$profit["profit_id"]])->update($update);
		return $res;
	}


	public function pay_notify(){
		$res = input("post.");
		file_put_contents("df.txt",var_export($res,true));
		$profit = Db::name("UserProfit")->where(["profit_form_no"=>$res["dsorderid"]])->find();
		if(empty($profit)){
			return "订单不存在";
		}
		if($profit["profit_type"] != "3"){
			return "订单不存在";
		}
		$update=[];
		$update["profit_paytime"]=time();
		$update["profit_back_msg"]=isset($res["errtext"]) ? $res["errtext"] : "成功";
		if($res["status"] == "00"){//成功
			$update["profit_type"]=2;
			$update["profit_balance"] = $res["currAccountBalance"];
			$update["profit_back_msg"]="提现成功";
		}elseif($res["status"] == "01"){//处理中
			$update["profit_type"]=4;
			$update["profit_balance"] = $res["currAccountBalance"];
		}elseif($res["status"] == "02"){//失败
			$update["profit_type"]=3;
			//返还金额
			Db::name("user")->where(["user_id"=>$profit["profit_uid"]])->setInc("user_moeny",$profit["profit_money"]);
		}elseif($res["status"] == "04"){//订单关闭
			$update["profit_type"]=5;
		}
		if(isset($res["frozenamount"]) && $res["frozenamount"]>0){
			$update["frozenamount"] = $res["frozenamount"];
		}
		$upres = Db::name("user_profit")->where(["profit_id"=>$profit["profit_id"]])->update($update);
		return $upres;
	}


	public function queryOrder($profit){
		$data=[];
		$data["ordersn"] = get_order_sn("",$profit["profit_uid"]);
		$data["dsorderid"] = $profit["profit_form_no"];
		$res = (new Hqdf)->queryOrder($data);
		//halt($res);
		$update=[];
		$update["profit_paytime"]=time();
		$update["profit_back_msg"]=isset($res["errtext"]) ? $res["errtext"] : "成功";
		if($res["status"] == "00"){//成功
			$update["profit_type"]=2;
			$update["profit_back_msg"]="提现成功";
		}elseif($res["status"] == "01"){//处理中
			$update["profit_type"]=4;
		}elseif($res["status"] == "02"){//失败
			$update["profit_type"]=3;
			//返还金额
			Db::name("user")->where(["user_id"=>$profit["profit_uid"]])->setInc("user_moeny",$profit["profit_money"]);
		}elseif($res["status"] == "04"){//订单关闭
			$update["profit_type"]=5;
		}

		$upres = Db::name("user_profit")->where(["profit_id"=>$profit["profit_id"]])->update($update);
		return $res;
	}
}